#!/bin/tcsh -ef
#############################################################################
#                                                                           #
# Title: Image Inventory                                                    #
#                                                                           #
# (C) 2dx.org, GNU Plublic License.                                         #
#                                                                           #
# Created..........: 03/01/2006                                             #
# Last Modification: 03/01/2006                                             #
# Author...........: 2dx.org                                                #
#                                                                           #
#############################################################################
#
# SORTORDER: 82
#
# MANUAL: This script will produce a list of *.mrc or other types of files, and show them in the <i>Images</I> pane. Select the kind of files you want to see.
#
# RESET: inventory_image_do = "0"
#
# DISPLAY: comment
#
#$end_local_vars
#
set bin_2dx = ""
set proc_2dx = ""
#
set inventory_image_do = ""
#
#$end_vars
#
set scriptname = 2dx_inventory
#
\rm -f LOGS/${scriptname}.results
\rm -f SCRATCH/inventory.txt
#
source ${proc_2dx}/initialize
#
echo "inventory_image_do = ${inventory_image_do}"
echo "<<@progress: 1>>"
#
if ( ${inventory_image_do} == "0" || ${inventory_image_do} == "7" ) then

  ${proc_2dx}/linblock "inventory of \*.mrc"
  echo dummy > 111Adummy.mrc
  \ls -1 *.mrc | grep -v "111Adummy.mrc" | sed -e "s/^/# IMAGE-IMPORTANT: /" >> SCRATCH/inventory.txt
  \rm -f 111Adummy.mrc

  echo "<<@progress: +5>>"

  if ( -d FFTIR ) then
    ${proc_2dx}/linblock "inventory of FFTIR/\*.mrc"
    echo dummy > FFTIR/111Adummy.mrc
    echo `\ls -1 FFTIR/*.mrc | wc -l` > tmp.tmp
    set lines = `cat tmp.tmp`
    if ( ${lines} != '1' ) then
      \ls -1 FFTIR/*.mrc | grep -v "111Adummy.mrc" | sed -e "s/^/# IMAGE-IMPORTANT: /" >> SCRATCH/inventory.txt
    endif
    \rm -f FFTIR/111Adummy.mrc
  else
    ${proc_2dx}/linblock "             FFTIR not existing"
  endif

  echo "<<@progress: +5>>"

  if ( -d SCRATCH || -l SCRATCH ) then
    ${proc_2dx}/linblock "inventory of SCRATCH/\*.mrc"
    echo dummy > SCRATCH/111Adummy.mrc
    echo `\ls -1 SCRATCH/*.mrc | wc -l` > tmp.tmp
    set lines = `cat tmp.tmp`
    if ( ${lines} != '1' ) then
      \ls -1 SCRATCH/*.mrc | grep -v "111Adummy.mrc" | sed -e "s/^/# IMAGE-IMPORTANT: /" >> SCRATCH/inventory.txt
    endif
    \rm -f SCRATCH/111Adummy.mrc
  else
    ${proc_2dx}/linblock "             SCRATCH not existing"
  endif

  echo "<<@progress: +5>>"

  if ( -d CUT ) then
    ${proc_2dx}/linblock "inventory of CUT/\*.mrc"
    echo dummy > CUT/111Adummy.mrc
    echo `\ls -1 CUT/*.mrc | wc -l` > tmp.tmp
    set lines = `cat tmp.tmp`
    if ( ${lines} != '1' ) then
      \ls -1 CUT/*.mrc | grep -v "111Adummy" | sed -e "s/^/# IMAGE-IMPORTANT: /" >> SCRATCH/inventory.txt
    endif
    \rm -f CUT/111Adummy.mrc
  else
    ${proc_2dx}/linblock "             CUT not existing"
  endif

endif

if ( ${inventory_image_do} == "1" || ${inventory_image_do} == "7" ) then

  ${proc_2dx}/linblock "inventory of \*.map"
  echo dummy > 111Adummy.map
  echo `\ls -1 *.map | wc -l` > tmp.tmp
  set lines = `cat tmp.tmp`
  if ( ${lines} != '1' ) then
    \ls -1 *.map | grep -v "111Adummy.map" | sed -e "s/^/# IMAGE-IMPORTANT: /" >> SCRATCH/inventory.txt
  endif
  \rm -f 111Adummy.map

  echo "<<@progress: +5>>"

  if ( -d FFTIR ) then
    ${proc_2dx}/linblock "inventory of FFTIR/\*.map"
    echo dummy > FFTIR/111Adummy.map
    echo `\ls -1 FFTIR/*.map | wc -l` > tmp.tmp
    set lines = `cat tmp.tmp`
    if ( ${lines} != '1' ) then
      \ls -1 FFTIR/*.map | grep -v "111Adummy.map" | sed -e "s/^/# IMAGE-IMPORTANT: /" >> SCRATCH/inventory.txt
    endif
    \rm -f FFTIR/111Adummy.map
  else
    ${proc_2dx}/linblock "             FFTIR not existing"
  endif

  echo "<<@progress: +5>>"

  if ( -d SCRATCH ) then
    ${proc_2dx}/linblock "inventory of SCRATCH/\*.map"
    echo dummy > SCRATCH/111Adummy.map
    echo `\ls -1 SCRATCH/*.map | wc -l` > tmp.tmp
    set lines = `cat tmp.tmp`
    if ( ${lines} != '1' ) then
      \ls -1 SCRATCH/*.map | grep -v "111Adummy.map" | sed -e "s/^/# IMAGE-IMPORTANT: /" >> SCRATCH/inventory.txt
    endif
    \rm -f SCRATCH/111Adummy.map
  else
    ${proc_2dx}/linblock "             SCRATCH not existing"
  endif

  echo "<<@progress: +5>>"

  if ( -d CUT ) then
    ${proc_2dx}/linblock "inventory of CUT/\*.map"
    echo dummy > CUT/111Adummy.map
    echo `\ls -1 CUT/*.map | wc -l` > tmp.tmp
    set lines = `cat tmp.tmp`
    if ( ${lines} != '1' ) then
      \ls -1 CUT/*.map | grep -v "111Adummy" | sed -e "s/^/# IMAGE-IMPORTANT: /" >> SCRATCH/inventory.txt
    endif
    \rm -f CUT/111Adummy.map
  else
    ${proc_2dx}/linblock "             CUT not existing"
  endif

endif

  echo "<<@progress: +5>>"

if ( ${inventory_image_do} == "2" || ${inventory_image_do} == "7" ) then
  if ( -d PS ) then
    ${proc_2dx}/linblock "inventory of PS/\*.ps"
    echo dummy > PS/111Adummy.ps
    echo `\ls -1 PS/*.ps | wc -l` > tmp.tmp
    set lines = `cat tmp.tmp`
    if ( ${lines} != '1' ) then
      \ls -1 PS/*.ps | grep -v "111Adummy" | sed -e "s/^/# IMAGE-IMPORTANT: /" >> SCRATCH/inventory.txt
    endif
    \rm -f PS/111Adummy.ps
  else
    ${proc_2dx}/linblock "             PS not existing"
  endif
  echo dummy > 111Adummy.ps
  echo `\ls -1 *.ps | wc -l` > tmp.tmp
  set lines = `cat tmp.tmp`
  if ( ${lines} != '1' ) then
    \ls -1 *.ps | grep -v "111Adummy" | sed -e "s/^/# IMAGE-IMPORTANT: /" >> SCRATCH/inventory.txt
  endif
  \rm -f 111Adummy.ps
  echo dummy > 111Adummy.PS
  echo `\ls -1 *.PS | wc -l` > tmp.tmp
  set lines = `cat tmp.tmp`
  if ( ${lines} != '1' ) then
    \ls -1 *.PS | grep -v "111Adummy" | sed -e "s/^/# IMAGE-IMPORTANT: /" >> SCRATCH/inventory.txt
  endif
  \rm -f 111Adummy.PS
endif


if ( ${inventory_image_do} == "3" || ${inventory_image_do} == "7" ) then
  if ( -d APH ) then
    ${proc_2dx}/linblock "inventory of APH/\*.aph"
    echo dummy > APH/111Adummy.aph
    echo `\ls -1 APH/*.aph | wc -l` > tmp.tmp
    set lines = `cat tmp.tmp`
    if ( ${lines} != '1' ) then
      \ls -1 APH/*.aph | grep -v "111Adummy" | sed -e "s/^/# IMAGE-IMPORTANT: /" >> SCRATCH/inventory.txt
    endif
    \rm -f APH/111Adummy.aph
    echo dummy > APH/111Adummy.hkl
    echo `\ls -1 APH/*.hkl | wc -l` > tmp.tmp
    set lines = `cat tmp.tmp`
    if ( ${lines} != '1' ) then
      \ls -1 APH/*.hkl | grep -v "111Adummy" | sed -e "s/^/# IMAGE-IMPORTANT: /" >> SCRATCH/inventory.txt
    endif
    \rm -f APH/111Adummy.hkl
  else
    ${proc_2dx}/linblock "             APH not existing"
  endif
endif

if ( ${inventory_image_do} == "4" || ${inventory_image_do} == "7" ) then
  ${proc_2dx}/linblock "inventory of \*.spt"
  echo dummy > 111Adummy.spt
  echo `\ls -1 *.spt | wc -l` > tmp.tmp
  set lines = `cat tmp.tmp`
  if ( ${lines} != '1' ) then
    \ls -1 *.spt | grep -v "111Adummy" | sed -e "s/^/# IMAGE-IMPORTANT: /" >> SCRATCH/inventory.txt
  endif
  \rm -f 111Adummy.spt
endif

if ( ${inventory_image_do} == "5" || ${inventory_image_do} == "7" ) then
  if ( -d ML ) then
    ${proc_2dx}/linblock "inventory of ML*\*.mrc"
    echo dummy > ML/111Adummy.spt
    echo `\ls -1 ML*/*.mrc | wc -l` > tmp.tmp
    set lines = `cat tmp.tmp`
    if ( ${lines} != '1' ) then
      \ls -1 ML*/*.mrc | grep -v "111Adummy" | sed -e "s/^/# IMAGE-IMPORTANT: /" >> SCRATCH/inventory.txt
    endif
    \rm -f ML/111Adummy.spt
  else
    ${proc_2dx}/linblock "             ML not existing"
  endif
endif

if ( ${inventory_image_do} == "6" || ${inventory_image_do} == "7" ) then
  ${proc_2dx}/linblock "inventory of \*.png"
  echo dummy > 111Adummy.png
  echo `\ls -1 *.png | wc -l` > tmp.tmp
  set lines = `cat tmp.tmp`
  if ( ${lines} != '1' ) then
    \ls -1 *.png | grep -v "111Adummy" | sed -e "s/^/# IMAGE-IMPORTANT: /" >> SCRATCH/inventory.txt
  endif
  \rm -f 111Adummy.png
endif


\rm -f tmp.tmp

echo "::Sorting"
sort SCRATCH/inventory.txt >> LOGS/${scriptname}.results
#\rm -f SCRATCH/inventory.txt

echo "<<@progress: 100>>"
##########################################################################
${proc_2dx}/linblock "${scriptname} - normal end."
##########################################################################
